﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Display.DialogUI.*;
    import org.aswing.*;
    import Transactions.*;
    import Classes.util.*;
    import Classes.sim.*;
    import Classes.effects.*;
    import Modules.quest.Managers.*;
    import Display.*;
    import GameMode.*;
    import Classes.actions.*;
    import Modules.stats.experiments.*;
    import Modules.stats.types.*;
    import com.zynga.skelly.util.*;
    import Modules.quest.Helpers.*;

    public class Residence extends SimpleHarvestableResource {

        private const RESIDENCE:String = "residence";

        protected var m_numResidents:int;
        protected var m_numOccupants:int;
        private var m_truck:Vehicle;
        protected var m_movingIn:Boolean = false;

        public function Residence(_arg1:String=null){
            super(_arg1);
            m_objectType = WorldObjectTypes.RESIDENCE;
            m_typeName = this.RESIDENCE;
            this.m_numResidents = m_item.populationBase;
            this.m_numOccupants = this.m_numResidents;
            m_isRoadVerifiable = true;
        }
        override protected function statsInit():void{
            super.statsInit();
            registerStatsActionName(TrackedActionType.HARVEST, "collect");
        }
        override public function loadObject(_arg1:Object):void{
            super.loadObject(_arg1);
            this.updateNpcNames();
        }
        override public function get npcNames():Array{
            var _local1:String = this.m_item.getRandomSpawnNpc();
            return (((_local1)!=null) ? [_local1] : super.npcNames);
        }
        protected function updateNpcNames():void{
            var _local5:int;
            var _local1:PseudoRandomNumberGenerator = new PseudoRandomNumberGenerator(int(this.getId()));
            var _local2:int = (this.m_numResidents * 2);
            if (_local2 < 1){
                _local2 = Global.gameSettings.getInt("npcTypesPerBuilding", 1);
            };
            var _local3:Array = [];
            var _local4:int;
            while (_local4 < _local2) {
                _local5 = _local1.nextInRange(m_npcNames.length);
                _local3.push(m_npcNames[_local5]);
                _local4++;
            };
            m_npcNames = _local3;
        }
        public function takeResident():Boolean{
            this.m_numOccupants--;
            if (this.m_numOccupants < 1){
                this.m_numOccupants = 0;
                return (true);
            };
            return (false);
        }
        public function returnResident():void{
            this.m_numOccupants++;
            if (this.m_numOccupants > this.m_numResidents){
                this.m_numOccupants = this.m_numResidents;
            };
        }
        public function hasOccupants():Boolean{
            return ((this.m_numOccupants > 0));
        }
        override public function getToolTipStatus():String{
            var _local2:Number;
            var _local3:Object;
            var _local1:String;
            if (isNeedingRoad){
                _local1 = ZLoc.t("Dialogs", "NotConnectedToRoad");
            } else {
                if (((((((!(isLocked())) && (!(Global.isVisiting())))) && (!(Global.world.isEditMode)))) && (!(this.m_movingIn)))){
                    if (m_state == STATE_PLANTED){
                        _local1 = ZLoc.t("Main", "BareResidence", {time:GameUtil.formatMinutesSeconds(getGrowTimeLeft())});
                        _local2 = Global.gameSettings.getNumber("populationScale", 1);
                        _local3 = {
                            curPop:(getPopulationYield() * _local2),
                            maxPop:(getItem().populationMax * _local2)
                        };
                        _local1 = (_local1 + ("\n" + ZLoc.t("Dialogs", "TT_PopulationCurOfMax", _local3)));
                    };
                } else {
                    if (((((((!(isLocked())) && (!(Global.isVisiting())))) && (!(Global.world.isEditMode)))) && (this.m_movingIn))){
                        _local1 = ZLoc.t("Main", "MovingIn");
                    };
                };
            };
            return (_local1);
        }
        override public function getCustomToolTipStatus():Component{
            var _local3:Number;
            var _local1:Component = super.getCustomToolTipStatus();
            var _local2:Boolean;
            if (_local1){
                if (((((((((!(isLocked())) && (!(Global.isVisiting())))) && (!(Global.world.isEditMode)))) && (!(this.m_movingIn)))) && ((m_state == STATE_PLANTED)))){
                    _local2 = true;
                };
                if (_local2){
                    _local3 = (getToolTipTextWidth(ZLoc.t("Main", "BareResidence", {time:"00:00:00"})) + 5);
                    if (_local3 > _local1.getPreferredWidth()){
                        _local1.setPreferredWidth(_local3);
                    };
                } else {
                    _local1.setPreferredSize(null);
                };
            };
            return (_local1);
        }
        override public function getToolTipAction():String{
            var _local2:int;
            var _local1:String = getGameModeToolTipAction();
            if (!_local1){
                if (isNeedingRoad){
                    return (null);
                };
                _local2 = m_item.npcsPerHarvest;
                if (Global.isVisiting()){
                    if (((!(areVisitorInteractionsExhausted)) && ((m_state == STATE_GROWN)))){
                        _local1 = ZLoc.t("Main", "RipeResidence", {people:_local2});
                    };
                } else {
                    if (((((((!(isLocked())) && (!(Global.world.isEditMode)))) && (!(m_isHarvesting)))) && (!(this.m_movingIn)))){
                        if (m_state == STATE_GROWN){
                            if (Global.player.checkEnergy(-(m_item.harvestEnergyCost), false)){
                                _local1 = ZLoc.t("Main", "RipeResidence", {people:_local2});
                            } else {
                                _local1 = ZLoc.t("Main", "NeedEnergy", {amount:m_item.harvestEnergyCost});
                            };
                        } else {
                            _local1 = null;
                        };
                    };
                };
            };
            return (_local1);
        }
        override public function onBuildingConstructionCompleted_PreServerUpdate():void{
            var _local1:Number = getGrowTimeDelta();
            this.m_plantTime = (GlobalEngine.getTimer() - _local1);
            this.setState(STATE_PLANTED);
            plantTime = GlobalEngine.getTimer();
            Global.world.citySim.recomputePopulation(Global.world, false);
            this.doMoveIn();
        }
        private function endPlayMoveIn():void{
            this.m_movingIn = false;
            this.updateArrow();
            reloadImage();
        }
        public function playMoveIn():void{
            var loopsound:* = null;
            this.m_movingIn = true;
            var nearRoad:* = Global.world.citySim.roadManager.findClosestRoad(getPosition());
            if (!nearRoad){
                this.endPlayMoveIn();
                return;
            };
            var startRoad:* = Global.world.citySim.roadManager.findRandomRoadOnScreen(nearRoad);
            this.m_truck = new Vehicle("yehaul", false);
            this.m_truck.setPosition(startRoad.getHotspot().x, startRoad.getHotspot().y);
            Global.world.citySim.npcManager.addVehicle(this.m_truck);
            var moveInTime:* = Global.gameSettings.getNumber("actionBarResMoveIn");
            var actions:* = this.m_truck.getStateMachine();
            actions.removeAllStates();
            actions.addActions(new ActionAnimationEffect(this.m_truck, EffectType.VEHICLE_POOF), new ActionFn(this.m_truck, function ():void{
                Sounds.play("tourbus_honk");
                loopsound = Sounds.play("tourbus_engine_loop", 0, Sounds.LOOPING);
            }), new ActionNavigate(this.m_truck, this, startRoad).setPathType(RoadManager.PATH_ROAD_ONLY), new ActionFn(this.m_truck, Curry.curry(function (_arg1:Residence):void{
                _arg1.unloadResidents();
            }, this)), new ActionFn(this.m_truck, function ():void{
                Sounds.stop(loopsound);
                Sounds.play("tourbus_stop");
                Sounds.play("peopleMoveIn");
            }), new ActionProgressBar(this.m_truck, this, ZLoc.t("Main", "MovingIn"), moveInTime), new ActionFn(this.m_truck, function ():void{
                Sounds.play("tourbus_start");
                loopsound = Sounds.play("tourbus_engine_loop", 0, Sounds.LOOPING);
                endPlayMoveIn();
            }), new ActionNavigate(this.m_truck, startRoad, this), new ActionFn(this.m_truck, function ():void{
                Sounds.stop(loopsound);
                Sounds.play("tourbus_stop");
            }), new ActionDie(this.m_truck));
            displayStatus(ZLoc.t("Main", "MovingIn"));
        }
        private function doMoveIn():void{
            var loopsound:* = null;
            this.m_movingIn = true;
            var nearRoad:* = Global.world.citySim.roadManager.findClosestRoad(getPosition());
            if (!nearRoad){
                this.finishMoveIn();
                return;
            };
            var startRoad:* = Global.world.citySim.roadManager.findRandomRoadOnScreen(nearRoad);
            this.m_truck = new Vehicle("yehaul", false);
            if (Global.guide.isActive()){
                this.m_truck.setPosition(-6.5, 34.5);
            } else {
                this.m_truck.setPosition(startRoad.getHotspot().x, startRoad.getHotspot().y);
            };
            Global.world.citySim.npcManager.addVehicle(this.m_truck);
            var moveInTime:* = Global.gameSettings.getNumber("actionBarResMoveIn");
            var actions:* = this.m_truck.getStateMachine();
            actions.removeAllStates();
            actions.addActions(new ActionAnimationEffect(this.m_truck, EffectType.VEHICLE_POOF), new ActionFn(this.m_truck, function ():void{
                Sounds.play("tourbus_honk");
                loopsound = Sounds.play("tourbus_engine_loop", 0, Sounds.LOOPING);
            }), new ActionNavigate(this.m_truck, this, startRoad).setPathType(RoadManager.PATH_ROAD_ONLY), new ActionFn(this.m_truck, Curry.curry(function (_arg1:Residence):void{
                _arg1.unloadResidents();
            }, this)), new ActionFn(this.m_truck, function ():void{
                Sounds.stop(loopsound);
                Sounds.play("tourbus_stop");
                Sounds.play("peopleMoveIn");
            }), new ActionProgressBar(this.m_truck, this, ZLoc.t("Main", "MovingIn"), moveInTime), new ActionFn(this.m_truck, function ():void{
                Sounds.play("tourbus_start");
                loopsound = Sounds.play("tourbus_engine_loop", 0, Sounds.LOOPING);
                finishMoveIn();
            }), new ActionNavigate(this.m_truck, startRoad, this), new ActionFn(this.m_truck, function ():void{
                Sounds.stop(loopsound);
                Sounds.play("tourbus_stop");
            }), new ActionDie(this.m_truck));
            displayStatus(ZLoc.t("Main", "MovingIn"));
        }
        protected function unloadResidents():void{
            if (Global.guide.isActive()){
                Global.world.citySim.npcManager.createMoverPeeps(this, 3);
            } else {
                Global.world.citySim.npcManager.createMoverPeeps(this, this.m_numResidents);
            };
        }
        protected function finishMoveIn():void{
            this.m_movingIn = false;
            Global.world.citySim.recomputePopulation(Global.world);
            doResourceChanges(0, 0, 0, 0, "", true, false, true, 0, Global.world.citySim.applyPopulationScale(m_item.populationBase));
            Global.world.citySim.npcManager.addNewResidence(this);
            this.updateArrow();
            reloadImage();
            spawnDoobers();
        }
        override protected function makeDoobers(_arg1:Number=1):Array{
            return (Global.player.processRandomModifiers(harvestingDefinition, this, true, m_secureRands));
        }
        override public function onPlayAction():void{
            if (this.isUxLockedByQuests){
                displayStatus(ZLoc.t("Main", "QuestUXLocked"));
                return;
            };
            if ((((((m_visitReplayLock > 0)) || (this.m_movingIn))) || (Global.isVisiting()))){
                return;
            };
            if (isNeedingRoad){
                super.enterMoveMode();
                return;
            };
            if (m_state == STATE_PLANTED){
                this.processPlantedState();
                return;
            };
            if (m_state != STATE_GROWN){
                return;
            };
            m_actionMode = PLAY_ACTION;
            var _local1:Number = m_item.harvestEnergyCost;
            if (!Global.player.checkEnergy(-(_local1))){
                displayStatus(ZLoc.t("Main", "NotEnoughEnergyRed"));
            } else {
                if (!m_isHarvesting){
                    this.initiateHarvestingSequence();
                };
            };
        }
        override public function onVisitPlayAction():void{
            if (isNeedingRoad){
                return;
            };
            m_actionMode = VISIT_PLAY_ACTION;
            if (m_state == STATE_GROWN){
                m_numVisitorInteractions++;
                this.initiateHarvestingSequence();
                trackVisitAction(TrackedActionType.COLLECT_RENT);
            };
        }
        override public function onVisitReplayAction(_arg1:TRedeemVisitorHelpAction):Number{
            super.onVisitReplayAction(null);
            if (m_state == STATE_GROWN){
                m_isHarvesting = true;
                setHighlighted(false);
                removeAnimatedEffects();
                removeStagePickEffect();
                GameTransactionManager.addTransaction(_arg1);
                this.harvest();
                return (DEFAULT_REPLAY_TIME);
            };
            return (0);
        }
        override public function isVisitorInteractable():Boolean{
            if (isNeedingRoad){
                return (false);
            };
            if (m_state != STATE_GROWN){
                return (false);
            };
            return (true);
        }
        private function initiateHarvestingSequence():void{
            m_isHarvesting = true;
            setHighlighted(false);
            removeAnimatedEffects();
            removeStagePickEffect();
            Global.world.citySim.pickupManager.enqueue("NPC_mailman", this);
        }
        override protected function get isUxLockedByQuests():Boolean{
            return (((!((Global.questManager == null))) && (!(Global.questManager.isUXUnlocked(GameQuestManager.QUEST_UX_RENT_UNLOCKED)))));
        }
        override public function setState(_arg1:String):void{
            removeAnimatedEffects();
            super.setState(_arg1);
            this.updateArrow();
        }
        override public function updateRoadState():void{
            super.updateRoadState();
            this.updateEffects();
        }
        private function updateEffects():void{
            if (((!(isNeedingRoad)) && (!(this.isUxLockedByQuests)))){
                this.createStagePickEffect();
            } else {
                removeAnimatedEffects();
                if (m_arrow){
                    m_arrow = null;
                };
                removeStagePickEffect();
            };
        }
        override public function cleanUp():void{
            removeStagePickEffect();
            super.cleanUp();
        }
        override protected function updateArrow():void{
            removeAnimatedEffects();
            if (((isNeedingRoad) || (Global.world.isEditMode))){
                m_arrow = null;
                return;
            };
            switch (m_state){
                case STATE_GROWN:
                    m_isHarvesting = false;
                    m_isHighlightable = true;
                    m_isShowStateTransition = true;
                    this.createStagePickEffect();
                    break;
                case STATE_PLANTED:
                    m_isHarvesting = false;
                    m_isHighlightable = false;
                    if (m_isShowStateTransition){
                        m_isShowStateTransition = false;
                    };
                    removeStagePickEffect();
                    break;
            };
        }
        override protected function createStagePickEffect():void{
            if ((((((((((m_state == STATE_GROWN)) && (!(isNeedingRoad)))) && (!(this.m_movingIn)))) && (!(this.m_clearLock)))) && (!(this.isUxLockedByQuests)))){
                if (!m_stagePickEffect){
                    m_stagePickEffect = (MapResourceEffectFactory.createEffect(this, EffectType.STAGE_PICK) as StagePickEffect);
                    m_stagePickEffect.setPickType(StagePickEffect.PICK_1);
                    m_stagePickEffect.queuedFloat();
                } else {
                    m_stagePickEffect.setPickType(StagePickEffect.PICK_1);
                    m_stagePickEffect.reattach();
                    m_stagePickEffect.queuedFloat();
                };
            };
        }
        override protected function updateDisabled():void{
        }
        override protected function setArrowPosition():void{
        }
        override public function onEditModeSwitch():void{
            this.updateArrow();
            reloadImage();
        }
        override public function getActionText():String{
            return (ZLoc.t("Main", "Collecting"));
        }
        override public function harvest():Boolean{
            var _local2:int;
            var _local3:int;
            var _local1:Boolean = super.harvest();
            if (_local1){
                if (Global.isVisiting()){
                    _local2 = Global.gameSettings.getInt("friendVisitResidenceRepGain", 1);
                    _local3 = Global.gameSettings.getNumber("friendHelpDefaultCoinReward", 10);
                    finalizeAndAwardVisitorHelp(VisitorHelpType.RESIDENCE_COLLECT_RENT, _local2, _local3);
                };
            };
            m_actionMode = NO_ACTION;
            return (_local1);
        }
        override protected function getGrownCursor():Class{
            if (((m_isHarvesting) || (areVisitorInteractionsExhausted))){
                return (null);
            };
            return (null);
        }
        override public function get isHighlightable():Boolean{
            var _local1:Boolean;
            var _local2:GameMode = Global.world.getTopGameMode();
            if ((_local2 is GMPlay)){
                if (isNeedingRoad){
                    _local1 = false;
                } else {
                    _local1 = m_isHighlightable;
                };
            } else {
                _local1 = true;
            };
            return (_local1);
        }
        override public function getHarvestTime():Number{
            return (Global.gameSettings.getNumber("actionBarResHarvest"));
        }
        override public function onSell():void{
            Global.world.citySim.recomputePopulation(Global.world);
            Global.world.citySim.npcManager.removeResidence(this);
        }
        public function get isMovingIn():Boolean{
            return (this.m_movingIn);
        }
        override public function onObjectDrop(_arg1:Vector3):void{
            Global.world.citySim.roadManager.updateResource(this);
            if (!isNeedingRoad){
                this.displayStatus(ZLoc.t("Main", "ConnectedToRoad"), "", 0xAA00);
            };
        }
        override public function getCursor():Class{
            var _local1:Class = super.getCursor();
            if (isNeedingRoad){
                _local1 = EmbeddedArt.hud_act_move;
            };
            return (_local1);
        }
        override public function getProgressBarStartFunction():Function{
            var harvestResource:* = null;
            harvestResource = this;
            return (function ():Boolean{
                var _local1:*;
                if (m_movingIn){
                    return (true);
                };
                if (!Global.isVisiting()){
                    if (m_actionMode != VISIT_REPLAY_ACTION){
                        switch (m_state){
                            case STATE_GROWN:
                                _local1 = m_item.harvestEnergyCost;
                                if (m_actionMode == VISIT_REPLAY_ACTION){
                                    _local1 = 0;
                                };
                                if ((((m_actionMode == PLAY_ACTION)) && (!(Global.player.checkEnergy(-(_local1)))))){
                                    displayStatus(ZLoc.t("Main", "NotEnoughEnergyRed"));
                                    return (false);
                                };
                                Sounds.playFromSet(Sounds.SET_HARVEST, harvestResource);
                                doEnergyChanges(-(_local1), new Array("energy", "expenditures", "collect_house", harvestingDefinition.name), false);
                                Global.player.heldEnergy = (Global.player.heldEnergy + _local1);
                                m_heldEnergy = _local1;
                                break;
                        };
                    } else {
                        Sounds.playFromSet(Sounds.SET_HARVEST, harvestResource);
                    };
                } else {
                    switch (m_state){
                        case STATE_GROWN:
                            Sounds.playFromSet(Sounds.SET_HARVEST, harvestResource);
                            break;
                    };
                };
                return (true);
            });
        }
        override public function getProgressBarEndFunction():Function{
            var superEndFunction:* = null;
            superEndFunction = (this.m_movingIn) ? null : super.getProgressBarEndFunction();
            return (function ():void{
                Global.player.heldEnergy = (Global.player.heldEnergy - m_heldEnergy);
                m_heldEnergy = 0;
                if (superEndFunction != null){
                    superEndFunction();
                };
            });
        }
        override public function getProgressBarCancelFunction():Function{
            return (function ():void{
                m_isHarvesting = false;
                updateArrow();
                Global.player.heldEnergy = (Global.player.heldEnergy - m_heldEnergy);
                m_heldEnergy = 0;
            });
        }
        override public function deferProgressBarToNPC():Boolean{
            return (false);
        }
        override public function getProgressBarOffset():Point{
            if (this.content){
                return (new Point(0, (this.content.height >> 1)));
            };
            return (new Point(0, 0));
        }
        override public function prepareForStorage():void{
            super.prepareForStorage();
            Global.world.citySim.recomputePopulation(Global.world);
        }
        override public function restoreFromStorage():void{
            super.restoreFromStorage();
            Global.world.citySim.recomputePopulation(Global.world);
        }
        protected function processPlantedState():void{
            var _local2:DisplayObject;
            var _local1:int = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_INSTANT_READY_RESIDENCES);
            if (((((!(Global.isVisiting())) && ((Global.player.level >= 6)))) && ((_local1 > 1)))){
                _local2 = new InstantReadyDialog(this);
                UI.displayPopup(_local2, true, "instantReadyCrops", false);
            };
        }

    }
}//package Classes 
